<template>
  <div class="goods-comment">
    <div class="head" v-if="obj">
      <div class="data">
        <p>
          <span>{{ obj.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ obj.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            @click="tapIsShow(v, i)"
            v-for="(v, i) in obj.tags"
            :key="i"
            href="javascript:;"
            :class="{ active: i == isShow }"
            >{{ v.title }}: {{ v.tagCount }}</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a
        @click="changeSort(null)"
        href="javascript:;"
        :class="{ active: reqParmas.sortField == null }"
        >默认</a
      >
      <a
        @click="changeSort('praiseCount')"
        href="javascript:;"
        :class="{ active: reqParmas.sortField == 'praiseCount' }"
        >最热</a
      >
      <a
        @click="changeSort('createTime')"
        href="javascript:;"
        :class="{ active: reqParmas.sortField == 'createTime' }"
        >最新</a
      >
    </div>
    <!-- 列表 -->
    <div class="list" v-for="(v, i) in commentObj" :key="i">
      <div class="item">
        <div class="user">
          <img :src="v.member.avatar" alt="" />
          <span>{{ formatNickname(v.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i
              v-for="ii in v.score"
              :key="ii + 3"
              class="iconfont icon-wjx01"
            ></i>

            <i
              v-for="ii in 5 - v.score"
              :key="ii + 2"
              class="iconfont icon-wjx02"
            ></i>
            <!-- 总数减去已有的就得出 未有的 -->
            <span
              class="attr"
              v-for="(item, ii) in v.orderInfo.specs"
              :key="ii"
            >
              {{ item.name }}： {{ item.nameValue }}
            </span>
          </div>
          <div class="text">
            {{ v.content }}
          </div>
          <!-- 使用图片预览组件 -->
          <GoodsCommentImg
            v-if="v.pictures.length"
            :pictures="v.pictures"
          ></GoodsCommentImg>
          <div class="time">
            <span>{{ v.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ v.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getComment, getCommentPage } from "@/api/goods";
import { inject, ref, reactive, watch, computed } from "vue";
import GoodsCommentImg from "./goodsCommentImg.vue";
export default {
  name: "GoodsComment",
  setup() {
    const goods = inject("goods");
    let obj = ref([]);
    // 显示评价的数据
    let commentObj = ref([]);
    const formatNickname = (nickname) => {
      return nickname.substr(0, 1) + "****" + nickname.substr(-1);
    };
    // 评论筛选状态
    let isShow = ref(0);
    getComment(goods.value.id).then((res) => {
      console.log(res);
      // 这里是因为 少两条 数据所以这样做
      res.result.tags.unshift({
        title: "有图",
        tagCount: res.result.hasPictureCount,
        type: "img",
      });
      res.result.tags.unshift({
        title: "全部评价",
        tagCount: res.result.evaluateCount,
        type: "all",
      });
      obj.value = res.result;
    });
    //    定义传入后台的子段
    const reqParmas = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null,
    });
    // 监听字传入后台的数据
    watch(
      () => reqParmas,
      () => {
        // 商品评价的接口
        getCommentPage(goods.value.id, reqParmas).then((res) => {
          commentObj.value = res.result.items;
          console.log(commentObj.value);
        });
      },
      { deep: true, immediate: true }
    );
    // 筛选评论
    function changeSort(v) {
      console.log(v);
      reqParmas.sortField = v;
      reqParmas.page = 1;
    }
    function tapIsShow(v, i) {
      console.log(v);
      isShow.value = i;
      if (v.type == "all") {
        reqParmas.hasPicture = false;
        reqParmas.tag = null;
      } else if (v.type == "img") {
        reqParmas.hasPicture = true;
        reqParmas.tag = null;
      } else {
        reqParmas.hasPicture = false;
        reqParmas.tag = v.title;
      }
    }
    return {
      obj,
      isShow,
      tapIsShow,
      changeSort,
      reqParmas,
      commentObj,
      formatNickname,
    };
  },
  components: { GoodsCommentImg },
};
</script>
<style scoped lang="scss">

.active {
  border-color: $xtxColor !important;
  background: $xtxColor !important;
  color: rgb(255, 255, 255) !important;
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
// 评价
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: $priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: $xtxColor;
            background: lighten($xtxColor, 50%);
            color: $xtxColor;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: $xtxColor;
      }
    }
  }
}
</style>
